package com.ruoyi.web.controller.finance;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.finance.domain.KltInvoicing;
import com.ruoyi.finance.domain.KltInvoicingDetail;
import com.ruoyi.finance.domain.VO.KltInvoicingVO;
import com.ruoyi.finance.domain.VO.StockVO;
import com.ruoyi.finance.service.KltInvoicingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.text.ParseException;
import java.util.List;

import static com.ruoyi.common.core.domain.AjaxResult.success;

/**
 * (KltInvoicing)表控制层
 *
 * @author makejava
 * @since 2025-02-13 15:09:01
 */
@RestController
@RequestMapping("finance/kltInvoicing")
public class KltInvoicingController  {
    /**
     * 服务对象
     */
    @Autowired
    private KltInvoicingService kltInvoicingService;

    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param kltInvoicing 查询实体
     * @return 所有数据
     */
    @GetMapping("list")
    public AjaxResult selectAll(Page<KltInvoicing> page, KltInvoicing kltInvoicing) {
        kltInvoicing.setIsDel("0");
        return success(this.kltInvoicingService.page(page, new QueryWrapper<>(kltInvoicing)));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public AjaxResult selectOne(@PathVariable Serializable id) {
        return success(this.kltInvoicingService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param kltInvoicing 实体对象
     * @return 新增结果
     */
    @PostMapping
    public AjaxResult insert(@RequestBody KltInvoicing kltInvoicing) {
        return success(this.kltInvoicingService.save(kltInvoicing));
    }

    /**
     * 修改数据
     *
     * @param kltInvoicing 实体对象
     * @return 修改结果
     */
    @PutMapping
    public AjaxResult update(@RequestBody KltInvoicing kltInvoicing) {
        return success(this.kltInvoicingService.updateById(kltInvoicing));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public AjaxResult delete(@RequestParam("idList") List<Long> idList) {
        return success(this.kltInvoicingService.removeByIds(idList));
    }
    /**
     * 根据合同销售开票情况统计
     *
     * @param  salesOrderId
     * @return 删除结果
     */

    @GetMapping("getSalesOrderInvoicingById")
    public AjaxResult getSalesOrderInvoicingById( Integer salesOrderId) {
        return success(this.kltInvoicingService.getSalesOrderInvoicingById((Integer) salesOrderId));
    }
    @PostMapping("saveInvoicingAndDetail")
    public AjaxResult saveInvoicingAndDetail( @RequestBody KltInvoicingVO kltInvoicingVO)   {
        KltInvoicing kltInvoicing = new KltInvoicing();
        kltInvoicing.setInvoiceNumber(kltInvoicingVO.getInvoiceNumber());
        kltInvoicing.setInvoiceDate(kltInvoicingVO.getInvoiceDate());
        kltInvoicing.setSalesOrderId(kltInvoicingVO.getSalesOrderId());
        List<StockVO> stockVOList = kltInvoicingVO.getStockVOList();
        return success(this.kltInvoicingService.saveInvoicingAndDetail(kltInvoicing,stockVOList));
    }

    @GetMapping("delInvoicing")
    public AjaxResult delInvoicing( String invoicingId) {
        return success(this.kltInvoicingService.delInvoicing( invoicingId));
    }


}

